﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace 读取配置文件
{
    internal class Program
    {
        /// <summary>
        /// 搭配DI使用
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            
            // 实例化DI
            ServiceCollection services = new ServiceCollection();
            // 配置文件构造类
            ConfigurationBuilder builder = new ConfigurationBuilder();
            // 在类中添加本地的配置文件
            builder.AddJsonFile("config.json", false, true);
            // 初始化配置文件
            IConfigurationRoot config = builder.Build();
            // 在DI中注册配置文件的服务
            services.AddOptions().Configure<Teacher>(e=>config.Bind(e));
            services.AddScoped<Test>();

            using(ServiceProvider sp = services.BuildServiceProvider())
            {
                while (true)
                {
                    using(IServiceScope scope = sp.CreateScope())
                    {
                        Test test = scope.ServiceProvider.GetRequiredService<Test>();
                        test.Run();
                        Console.ReadKey();
                        test.Run();
                        Console.ReadKey();
                    }
                }

            }


            Console.ReadKey();
        }
    }
}
